package org.example;

import com.mysql.cj.jdbc.MysqlDataSource;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 九转苍翎
 * Date: 2025/7/16
 * Time: 17:17
 */
public class Main {
    public static Connection getConnection() throws SQLException {
        MysqlDataSource dataSource = new MysqlDataSource();
        dataSource.setUrl("jdbc:mysql://127.0.0.1:3306/java117?characterEncoding=utf8&useSSL=False");
        dataSource.setUser("root");
        dataSource.setPassword("123456");
        return dataSource.getConnection();
    }

    public static void main(String[] args) throws SQLException {
        Connection connection = Main.getConnection();
        String sql = "insert into jdbc (id,name,age) values(?,?,?)";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setInt(1, 1);
        preparedStatement.setString(2, "张三");
        preparedStatement.setInt(3, 18);
        preparedStatement.executeUpdate();
        preparedStatement.close();
        connection.close();
    }

    public static void main2(String[] args) throws SQLException {
        Connection connection = Main.getConnection();
        String sql = "select * from jdbc";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        ResultSet resultSet = preparedStatement.executeQuery();
        while (resultSet.next()) {
            System.out.println(resultSet.getString(1) + ","
                    + resultSet.getString(2) + ","
                    + resultSet.getString(3));
        }
        preparedStatement.close();
        connection.close();
    }
}